package com.db.demo.test.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Ribbon
 * @Date 2019/1/10 17:11
 **/
@Component
public class RedisLinsterConf {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Autowired
    private TopicMessageListener topicMessageListener;

    @Bean
    MessageListenerAdapter messageListenerAdapter() {
        return new MessageListenerAdapter(topicMessageListener);
    }
    @Bean
    RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        Map<MessageListener, Collection<? extends Topic>> map = new HashMap<>();
        map.put(messageListenerAdapter(), Arrays.asList(new ChannelTopic("__keyevent@0__:expired")));
        container.setMessageListeners(map);
        return container;
    }


}
